#include "StdAfx.h"
#include "Helpers.h"
#include "GroupUnitTests.h"
#include "VertexDirectory.h"
#include "GraphColoring.h"

// create group, insert and remove vertices to it 
TestUtils::TestResult createAndChangeGroup()
{
	TestUtils::TestResult result;
	int n=8;
	int w=5;
	VertexDirectory::Clear();

	bool nameArr1[] = {0,0,0,1,1,1,1,1};
	vector<bool> name1 (nameArr1, nameArr1+sizeof(nameArr1)/sizeof(bool));
	Vertex* vertex1 = new Vertex(name1);
	VertexDirectory::AddVertex(vertex1);
	
	bool nameArr2[] = {1,1,0,0,0,1,1,1};
	vector<bool> name2 (nameArr2, nameArr2+sizeof(nameArr2)/sizeof(bool));
	Vertex* vertex2 = new Vertex(name2);
	VertexDirectory::AddVertex(vertex2);
	
	bool nameArr3[] = {1,1,1,0,1,1,0,0};
	vector<bool> name3 (nameArr3, nameArr3+sizeof(nameArr3)/sizeof(bool));
	Vertex* vertex3 = new Vertex(name3);
	VertexDirectory::AddVertex(vertex3);
	
	set<vector<bool> >* vertices = VertexDirectory::CreateAllVertices(n,w);
	VertexDirectory::FillVertexDirectory(vertices);

	Group group(0);

	try
	{
		
		group.InsertNewVertex(vertex1);
		group.InsertNewVertex(vertex2);
		group.InsertNewVertex(vertex3);
		group.RemoveVertex(vertex3);
		group.RemoveVertex(vertex1);
		group.RemoveVertex(vertex2);
		result = TestUtils::TestPassed;
	}
	catch (GraphColoringException e)
	{
		result = TestUtils::VerifyIsTrue(false, "exeption");
	}

	return result;
}

// check that group breacks correctly
TestUtils::TestResult testBreakGroup()
{
	
	TestUtils::TestResult result;
	
	int n=8;
	int w=5;
	VertexDirectory::Clear();
	bool nameArr1[] = {0,0,0,1,1,1,1,1};
	vector<bool> name1 (nameArr1, nameArr1+sizeof(nameArr1)/sizeof(bool));
	Vertex* vertex1 = new Vertex(name1);
	VertexDirectory::AddVertex(vertex1);
	
	bool nameArr2[] = {1,1,0,0,0,1,1,1};
	vector<bool> name2 (nameArr2, nameArr2+sizeof(nameArr2)/sizeof(bool));
	Vertex* vertex2 = new Vertex(name2);
	VertexDirectory::AddVertex(vertex2);
	
	bool nameArr3[] = {1,1,1,0,1,1,0,0};
	vector<bool> name3 (nameArr3, nameArr3+sizeof(nameArr3)/sizeof(bool));
	Vertex* vertex3 = new Vertex(name3);
	VertexDirectory::AddVertex(vertex3);
	
	Group group(0);

	set<vector<bool> >* vertices = VertexDirectory::CreateAllVertices(n,w);
	VertexDirectory::FillVertexDirectory(vertices);
	list<Vertex*> *removed = new list<Vertex*>();

	try
	{
		
		group.InsertNewVertex(vertex1);
		group.InsertNewVertex(vertex2);
		group.InsertNewVertex(vertex3);
		
		Group *newGroup = group.BreakGroup(40, removed);
		newGroup->RemoveVertex(vertex1); // the only one in the new group after breaking
		
		group.RemoveVertex(vertex3); // check that original group is ok
		if (!group.IsVertexInGroup(vertex1->Name()))
			throw GraphColoringException("group after breaking - not ok!!");
		group.RemoveVertex(vertex1);
		group.RemoveVertex(vertex2);
		
		//check that the removed list containes the removed vertecies
		if(removed->size() != 2)
			throw GraphColoringException("wrong number of vertices in removed vertex list. group after breaking - not ok!!");

		list<Vertex*>::iterator it = removed->begin();
		for(; it != removed->end(); it++)
		{
			if (*it != vertex2  && *it != vertex3)
				throw GraphColoringException("wrong vertices in removed vertex list. group after breaking - not ok!!");
		}
		
		delete newGroup; 
		delete removed;

		result = TestUtils::TestPassed;
	}
	catch (GraphColoringException)
	{
		result = TestUtils::VerifyIsTrue(false, "exeption");
	}
	catch(exception)
	{
		result = TestUtils::VerifyIsTrue(false, "exeption");
	}

	return result;
}